package com.aaa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aaa.dao.TesterFpaDao;
import com.aaa.dao.ViewTestResultFpaDao;
import com.aaa.entity.TestResultFpa;
import com.aaa.dao.TestResultFpaDao;
import com.aaa.entity.TesterFpa;
import com.aaa.entity.TesterVo;
import com.aaa.entity.ViewTestResultFpa;
import com.aaa.service.TestResultFpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (TestResultFpa)表服务实现类
 *
 * @author
 * @since 2022-07-07 11:07:37
 */
@Service("testResultFpaService")
public class TestResultFpaServiceImpl implements TestResultFpaService {
    @Resource
    private TestResultFpaDao testResultFpaDao;
    @Autowired
    private ViewTestResultFpaDao viewTestResultFpaDao;

    @Autowired
    private TesterFpaDao testerFpaDao;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TestResultFpa queryById(Integer id) {
        return this.testResultFpaDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param testResultFpa 筛选条件
     * @param pageRequest   分页对象
     * @return 查询结果
     */
    @Override
    public Page<TestResultFpa> queryByPage(TestResultFpa testResultFpa, PageRequest pageRequest) {
        long total = this.testResultFpaDao.count(testResultFpa);
        return new PageImpl<>(this.testResultFpaDao.queryAllByLimit(testResultFpa, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param testResultFpa 实例对象
     * @return 实例对象
     */
    @Override
    public TestResultFpa insert(TestResultFpa testResultFpa) {
        this.testResultFpaDao.insert(testResultFpa);
        return testResultFpa;
    }

    /**
     * 修改数据
     *
     * @param testResultFpa 实例对象
     * @return 实例对象
     */
    @Override
    public TestResultFpa update(TestResultFpa testResultFpa) {
        this.testResultFpaDao.update(testResultFpa);
        return this.queryById(testResultFpa.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.testResultFpaDao.deleteById(id) > 0;
    }

    @Override
    public String saveTestResult(List<Map<String, Object>> answerList, TesterVo tester) {
        //保存测试者信息
        TesterFpa testerFpa = new TesterFpa();
        //Bean之间的转换主要是相同属性的复制
        /**
         * 复制Bean对象属性<br>
         * 限制类用于限制拷贝的属性，例如一个类我只想复制其父类的一些属性，就可以将editable设置为父类
         *
         * @param source           源Bean对象
         * @param target           目标Bean对象
         * @param ignoreProperties 不拷贝的的属性列表
         */
        BeanUtil.copyProperties(tester, testerFpa, "id");
        testerFpaDao.insert(testerFpa);
        //获取自增之后的主键
        Integer testerId = testerFpa.getId();
        //保存测试结果信息
        int i = this.insertBatch(answerList, testerId);
        if (i > 0) {
            return this.seleResult(testerId);
        }
        return null;
    }

    @Override
    public int insertBatch(List<Map<String, Object>> answerList, Integer testerId) {
        return testResultFpaDao.insertBatch(answerList, testerId);
    }

    @Override
    public String seleResult(Integer testerId) {

        ViewTestResultFpa viewFpa = viewTestResultFpaDao.seleResultFpa(testerId);
        double red = viewFpa.getRedCount();
        double yellow = viewFpa.getYellowCount();
        double blue = viewFpa.getBlueCount();
        double green = viewFpa.getGreenCount();
        if (red > yellow && red > blue && red > green) {
            return "/views/front/red.jsp";
        } else if (yellow > red && yellow > blue && yellow > green) {
            return "/views/front/yellow.jsp";
        } else if (blue > red && blue > yellow && blue > green) {
            return "/views/front/blue.jsp";
        } else if (green > red && green > yellow && green > blue) {
            return "/views/front/green.jsp";
        } else {
            return "/views/front/All.jsp";
        }

    }
}

